﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

using LoginServer.Manager;
using LoginServer.Virtual_Objects.User;

namespace LoginServer.Networking.Packets
{
    class PACKET_SERVER_LIST : Packet
    {
        public enum errorCodes : int
        {
            Nickname = 72000,
            WrongUser = 72010,
            WrongPW = 72020,
            AlreadyLoggedIn = 72030,
            Banned = 73050,
            BannedTime = 73020,
            AlreadyUsedNick = 74070
        }

        public int GetOnlineUsers()
        {
            int Players = 0;
            int[] _OnlineID = DB.runReadColumn("SELECT * FROM users WHERE online='1'", 0, null);
            for (int I = 0; I < _OnlineID.Length; I++)
                Players++;
            return Players;
        }

        public PACKET_SERVER_LIST(int UserID, String Username, String Password, String Nickname, int SessionID, int Rank)
        {
            setID(4352); // PacketID
            addBlock(1); // Error Code - 1 = Success
            addBlock(UserID); // UserID
            addBlock(0); // ?
            addBlock(Username); // Username
            addBlock(Password); // Password
            addBlock(Nickname); // Nickname
            addBlock(1); // Unique ID 1 - Default = 1
            addBlock(0); // Unique ID 2 - Default = 0
            addBlock(SessionID); // SessionID
            addBlock(Rank > 2 ? 999 : 0); // AccessLevel
            addBlock(Password); // PassPort - C2+
            addBlock(Program.ServerToShow);
            //
            addBlock("36"); // ServerID
            addBlock(Program.ServerName); // Server Name
            addBlock(Program.ServerIP); // Server IP
            addBlock(5340); // Port
            addBlock(GetOnlineUsers() * 25); // Server Playercount
            addBlock(0); // Flag - 0=All - 1=Adult 2=Clan(Hidden)

            //Clan shit here :(TODO)
            addBlock(-1);
            addBlock(-1);
            addBlock(-1);
            addBlock(-1);
            //End Clan :(TODO)
            addBlock(0);
            addBlock(0);
        }

        public PACKET_SERVER_LIST(errorCodes errCode, params object[] oParams)
        {
            setID(4352);
            addBlock(((int)errCode));

            foreach (object tParam in oParams)
                addBlock(tParam);
        }
    }
}
